;(function($, window, document) {
	"use strict";
	$.sangerExtend = function(){};
	var deBug={},TOOLS = {deBug:function(){console.log(deBug);}};	
	$.extend({
		sanger: function(fnName, param1, param2, param3,param4,param5) {
			var fn = $.extend({}, sanger, $.sangerExtend.prototype,TOOLS),START,END,handler;
            START = new Date().getTime();
            if (fn[fnName] == undefined){
				console.log(fnName);
				return false;
			}
            handler = fn[fnName](param1, param2, param3,param4,param5);
            END = new Date().getTime();
            if(typeof $.sangerExtend.prototype[fnName] !== 'undefined'){
                deBug[fnName] = (END - START) + 'ms/Extend';
            }else{
                deBug[fnName] = (END - START) + 'ms';
            };
			return handler;
		}
	});

	var sanger = {
		//新表格运行方法
		getTable:function(table_class,data,fn) {
			//表格读取之后移除loading
			$('.'+table_class).find('.chart-loading').remove();
			//创建筛选层
			// $('.'+table_class).append('<div class="layui-table-filter-box"></div>'); /*layui-anim layui-anim-upbit*/
            if($('.'+table_class).find('.layui-table-filter-box').length == 0){
                $('.'+table_class).append('<div class="layui-table-filter-box layui-anim layui-anim-upbit"></div>');
            }else{
            	$('.'+table_class).find('.layui-table-filter-box').remove();
            	$('.'+table_class).append('<div class="layui-table-filter-box layui-anim layui-anim-upbit"></div>');
            }			
			layui.use(['layer','table','form','tableCheckBoxUtil'],function(){
				var layer = layui.layer,
					table = layui.table,
					form = layui.form,
					tableCheckBoxUtil = layui.tableCheckBoxUtil;

				console.log('新表格',table_class,data)
				if (typeof(table_class) == 'undefined') {
					layer.alert('表格：显示区域没有标识',{icon:7});
					return false;
				}
				if (data.url == '') {
					layer.alert('表格：请求url不能为空',{icon:7});
					return false;
				}
				var params = {};
				for (var i in data.params) {
					params[data.params[i].name] = data.params[i].value;
				}

				console.log('table名称：',table_class)
				console.log('新表格参数',params)
				var flag = true;
				var _fieldName;	//跨页选择需指定某个值，默认数据中返回的第一个
				var _params = new Object;
				var laySize,	//表格尺寸
					layEven,	//表格隔行换色
					laySkin,	//表格边框风格
					layPage,    //表格分页开启
					layHeight,	//表格高度
					layToolbar,	//表格工具条是否开启
					layTotalRow;   //开启合计

				//表格风格自定义
				if(data.size) laySize = data.size;
				if(data.skin) laySkin = data.skin;
				if(data.even) layEven = data.even;
				if(data.height) layHeight = data.height;
				layPage = data.page == false ? data.page : true;
				layTotalRow = data.totalRow ? data.totalRow : false;
				table.render({
					id:'ID_'+table_class,
					elem:'#'+table_class,
					url: data.url,
					page: layPage,
					limit:50,
					limits:[50,100,200,300]	,				
					toolbar:data.toolbar==false?false:'#'+table_class+'_toolbar',//表格工具栏开启状态
					autoSort: false,
					defaultToolbar:['filter'],//只显示筛选，下载功能 exports
					size:laySize,
					skin:laySkin,
					even:layEven,
					height:layHeight,
					totalRow:layTotalRow,
					where:params,
					response: { //自定义数据相应名称
						statusName: 'status',
						statusCode: true
					},
					request: { //从新设定传参字段
						limitName: 'pageSize'
					},
					parseData: function(res){ //res 即为原始返回的数据
						console.log('原始返回数据',res)
						// return {
						// 	"code": 0, //解析接口状态,接口返回中无此状态，手动设置成功状态码
						// 	"count": res.total, //解析数据长度
						// 	"data": res.rows//解析数据列表
						// };
					},				
					cols:[data.columns],
					done: function (data) {
						console.log('处理后返回数据',data)
						if(data.totalRow != undefined && JSON.stringify(data.totalRow) != '{}'){
							for(var k in data.totalRow){
								$(this.elem[0]).next().find('.layui-table-total td[data-field="'+k+'"]>div').html(data.totalRow[k]);
							}
						}
						//这里需要的数据中第一个属性名称只在第一次传递，之后不再需要执行
						if(flag){
							if(data.data.length > 0){
								_fieldName = Object.keys(data.data[0])[0];
								_params['checked'] = Object.keys(data.data[0])[0];
								fn(_params);//执行当前页面中表格的一些事件,传参数据中第一个属性名称(id)，用于获取编号显示选中行数据
							}
						}
						tableCheckBox();
						flag = false;
					}
				});

				//表格跨页选择插件
				function tableCheckBox(){
					//初始化表格行选中状态
					tableCheckBoxUtil.checkedDefault({
						gridId: 'ID_'+table_class
						, fieldName:_fieldName
					});
					// 初始化表格分页设置
					tableCheckBoxUtil.init({
						gridId: 'ID_'+table_class
						, filterId: table_class+'_filter'
						, fieldName:_fieldName//checkbox选择指定字段表
					});
				}
			});
		},
	};

})(jQuery, window, document);